//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Xml;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("XPath Conversion")]
    [Description("Activity to select a part of the document node using the X Path string.")]
    [WorkflowCategory("Utilities")]
    public class XPathConversionActivity : Activity
    {
        #region Dependency Properties

        public static DependencyProperty InputXmlDocProperty = DependencyProperty.Register("InputXmlDoc",
            typeof(XmlDocument), typeof(XPathConversionActivity));

        [RequiredInputParam]
        [Name("XmlDocument")]
        [Description("Xml Document to be Parsed.")]
        public XmlDocument InputXmlDoc
        {
            get
            {
                return ((XmlDocument)(base.GetValue(XPathConversionActivity.InputXmlDocProperty)));
            }
            set
            {
                base.SetValue(XPathConversionActivity.InputXmlDocProperty, value);
            }
        }

        public static DependencyProperty XQueryProperty = DependencyProperty.Register("XQuery",
            typeof(string), typeof(XPathConversionActivity));

        [RequiredInputParam]
        [Name("XPathQuery")]
        [Description("XPath query string.")]
        public string XQuery
        {
            get
            {
                return ((string)(base.GetValue(XPathConversionActivity.XQueryProperty)));
            }
            set
            {
                base.SetValue(XPathConversionActivity.XQueryProperty, value);
            }
        }

        public static DependencyProperty OutputXmlDocProperty = DependencyProperty.Register("OutputXmlDoc",
            typeof(XmlDocument), typeof(XPathConversionActivity));

        [OutputParam]
        [Name("OutXmlDocument")]
        [Description("Transformed Xml Document.")]
        public XmlDocument OutputXmlDoc
        {
            get
            {
                return ((XmlDocument)(base.GetValue(XPathConversionActivity.OutputXmlDocProperty)));
            }
            set
            {
                base.SetValue(XPathConversionActivity.OutputXmlDocProperty, value);
            }
        }

        #endregion

        #region Overidden Methods

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            if (null == this.InputXmlDoc || null == this.XQuery)
                return ActivityExecutionStatus.Faulting;

            this.OutputXmlDoc = new XmlDocument();
            XmlNode node = this.InputXmlDoc.SelectSingleNode(this.XQuery);
            this.OutputXmlDoc.LoadXml(node.OuterXml);

            return ActivityExecutionStatus.Closed;
        }

        #endregion
    }
}
